package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import client.Main;
import client.MemberAccount;

public class ChangePriv_GUI extends JFrame implements ActionListener {

	public String[] privs = {" ","0","1","2","3","4","5","6"};
	public String[] columnNames = {"User ID", "Name" ,"Privilege"}; 
	public MemberAccount ma = null;
	public Object[][] data = null;
	public int uid=-1;

	
	//Buttons
	public JButton btnBack;
	public JButton btnLogOut;
	public JButton btnOK;
	public JButton btnSet;
	
	//panels
	JPanel Mainpanel;
	
	//labels
	JLabel lblHeader;
	JLabel lblText;
	JLabel lblText1;
	
	//combo box
	JComboBox priv;
	
	//input text
	JTextArea textInput;
	
	//table
	JTable table;
	JScrollPane scroll;
	
	public ChangePriv_GUI(){
		
		super();
		this.setSize(500,400);
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);			
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setTitle("Zer Lah - System Settings");
		this.add(getMainPanel());
		this.setVisible(true);
	}
	
	JPanel getMainPanel() 
	{	
		if (Mainpanel == null){
			Mainpanel = new JPanel();
			Mainpanel.setSize(500, 400);
			Mainpanel.setLayout(null);
			Mainpanel.add(getLabelHeader());
			Mainpanel.add(getLblText());
			Mainpanel.add(getLblText1());
			Mainpanel.add(getBtnBack());
			Mainpanel.add(getBtnLogOut());
			Mainpanel.add(getTextInput());
			Mainpanel.add(getBtnOK());
			Mainpanel.add(getCombo());
			Mainpanel.add(getBtnSet());
		}
		return Mainpanel;
	}
	
	JButton getBtnBack(){
			
			if (btnBack == null){
				btnBack = new JButton();
				btnBack.setBounds(380, 20, 100, 40);
				btnBack.setText("Back");
			}
			return btnBack;
		}
	
	JButton getBtnLogOut() {
		
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.addActionListener(this);
			btnLogOut.setText("LogOut");
		}
		return btnLogOut;
	}
	
	JButton getBtnOK(){
			
			if(btnOK == null){
				btnOK = new JButton();
				btnOK.setBounds(430, 160, 60, 40);
				btnOK.setText("OK");
				btnOK.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						if(textInput.getText().isEmpty())
							JOptionPane.showMessageDialog(null, "You must insert a user ID!");
						else
						{
							uid =Integer.parseInt(textInput.getText());
							ma = new MemberAccount(uid);
							data = ma.OK(2);
							if(data.length==0)
							{
								JOptionPane.showMessageDialog(null, "User does not exist!");
								if(table!=null)
								{
									remove(table);
									remove(scroll);
									table.setVisible(false);
									scroll.setVisible(false);
									Mainpanel.repaint();
								}
							}
							else
								Mainpanel.add(getTable());
						}}});
			}
			return btnOK;
	}
	
	JButton getBtnSet(){
		
		if(btnSet == null){
			btnSet = new JButton();
			btnSet.setBounds(430, 270, 60, 40);
			btnSet.setText("Set");
			btnSet.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					if(uid==-1)
						JOptionPane.showMessageDialog(null, "You must insert a user ID!");
					else if(priv.getSelectedItem().equals(" "))
						JOptionPane.showMessageDialog(null, "You must select a privilege!");
						
						else
							{	
								ma = new MemberAccount(uid);
								ma.Set((String) priv.getSelectedItem());
								JOptionPane.showMessageDialog(null, "User "+uid+" privilege has been changed!");
								uid=-1;
							}
				}});
		}
		return btnSet;
	}
	
	
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Privilege Changing");
			lblHeader.setBounds(130, 100, 230, 40);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JLabel getLblText()
	{
		if(lblText==null)
		{
			lblText=new JLabel();
			lblText.setText("Insert the required user ID:");
			lblText.setBounds(20, 160, 220, 40);
			lblText.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblText;
	}
	
	JLabel getLblText1()
	{
		if(lblText1==null)
		{
			lblText1=new JLabel();
			lblText1.setText("Choose a new privilege:");
			lblText1.setBounds(20, 270, 220, 40);
			lblText1.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblText1;
	}
	
	JTextArea getTextInput()
	{
		textInput= new JTextArea(60,1);
		textInput.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		textInput.setBounds(270,160,130,40);
		return textInput;
	}
	
	JComboBox getCombo()
	{
		if(priv==null)
		{
			priv=new JComboBox();
			for(int i=0;i<8;i++)
				priv.addItem(privs[i]);
				priv.setBounds(330, 270, 70, 40);
		}
		return priv;
	}
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(data, columnNames){
			  public boolean isCellEditable(int rowIndex, int mColIndex) {
			      return false;
			  }};
		  table = new JTable(model);
		  
		/*  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(200);
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(1);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(1);*/
	      scroll = new JScrollPane(table); 
	      scroll.setBounds(20, 210, 450, 50);
	      table.setBounds(20, 210, 450, 50);
	      return scroll; 
	}

	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
	}

}

